namespace Maple.Branch.Permissions
{
    using Microsoft.Extensions.DependencyInjection;
    using Options;
    using TestDatas;
    using Volo.Abp;
    using Volo.Abp.Autofac;
    using Volo.Abp.Modularity;
    using Volo.Abp.Threading;

    [DependsOn(typeof(PermissionsDomainModule),
        typeof(AbpTestBaseModule),
        typeof(AbpAutofacModule))]
    public class PermissionsTestBaseModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.Configure<PermissionProviderOptions>(options =>
            {
                options.ManagementProviders.Add<TestPermissionManagementProvider>();
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            SeedTestData(context);
        }

        private static void SeedTestData(ApplicationInitializationContext context)
        {
            using var scope = context.ServiceProvider.CreateScope();

            AsyncHelper.RunSync(async () =>
            {
                await scope.ServiceProvider
                .GetRequiredService<PermissionsTestDataBuilder>()
                .BuildAsync();
            });
        }
    }
}
